import {
  yesOrNoData
} from '../config/constants'
export function html2Text (val) {
  const div = document.createElement('div')
  div.innerHTML = val
  return div.textContent || div.innerText
}

export function dateFormat (time, cFormat) {

}

export function orderStatus (status) {
  let data = {
    '0': '待付款',
    '1': '待发货',
    '2': '待收货',
    '4': '待评论',
    '8': '已评论',
    '16': '已退款',
    '32': '退款处理中',
    '128': '退款申请中',
    '256': '退款拒绝',
    '64': '退款失败',
    '-1': '已取消',
    '99': '已取消'
  }
  return data[status + '']
}
export function apiUrl (url) {
  return process.env.hostAddress + url
}
export function moneyFormat (number) {
  return number.toFixed(2)
}
export function yesNoFilter (v) {
  return yesOrNoData[v]
}
export function authStatusFilter (status) {
  return {
    '0': '待审核',
    '1': '审核中',
    '2': '审核通过',
    '9': '审核不通过'
  }[status.toString()]
}

export function goodsStatus (status) {
  return ['出售中', '下架区', '仓库中'][parseInt(status)]
}

export function tagStatus (status) {
  if (status == 9) {
    return 'danger'
  }
  return ['success', 'info', 'warning', 'danger'][parseInt(status)]
}

export function absUrl (url) {
  if (!url) {
    return url
  }
  if (url.indexOf('://') != -1) {
    return url
  }
  if (url[0] != '/') {
    url = '/' + url
  }
  if (url.indexOf('/images/') != -1 || url.indexOf('/imgs/') != -1) {
    return url
  }
  if (url.indexOf('/oss-image/') != -1) {
    return 'http://weixin-ostorage01.oss-cn-shenzhen.aliyuncs.com' + url
  }
  return 'https://weixina01.bppc.com.cn/zy-admin/UploadFiles/zy/product' + url
}

export function paymentTypeName (type) {
  const data = {
    0: '积分支付',
    1: '微信支付',
    2: '微信+积分支付',
    49: '转账',
    75: '支票'
  }
  return data[parseInt(type)]
}

export function parseTime (time, cFormat) {
  if (typeof time === 'string') {
    return time
  }
  if (arguments.length === 0 || !time) {
    return null
  }

  if ((time + '').length === 10) {
    time = +time * 1000
  }

  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    date = new Date(parseInt(time))
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

export function salePropsFormat (prop) {
  if (prop && prop != '{}') {
    prop = JSON.parse(prop)
    const rt = []
    for (const key in prop) {
      rt.push(key + ':' + prop[key])
    }
    return '(' + rt.join(',') + ')'
  }
  return ''
}

export function numberFormat (v) {
  if (!v) {
    return v
  }
  return Math.round(v * 100) / 100
}

function dw (v) {
  return v > 9 ? v : '0' + v
}
export function timeFormat (time) {
  return time.year + '-' + dw(time.monthValue) + '-' + dw(time.dayOfMonth) + ' ' + dw(time.hour) + ':' + dw(time.minute) + ':' + dw(time.second)
}
